/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MaestroAlmacenes.java
 *
 * Created on 20/06/2012, 01:02:35 AM
 */
package com.camachosanchez.gui;

import com.camachosanchez.bean.Almacenes;
import com.camachosanchez.controler.jdbc.AlmacenesJdbcControler;
import com.camachosanchez.controler.jpa.AlmacenesJpaController;
import com.camachosanchez.gui.table.ModeloTablaAlmacenes;
import com.camachosanchez.gui.table.RenderizadorTabla;
import com.camachosanchez.gui.table.TableSorter;
import com.camachosanchez.util.EstadosJDialog.EstadoJDialog;
import com.camachosanchez.util.GUIUtil;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.event.InternalFrameEvent;
import net.coderazzi.filters.gui.TableFilterHeader;

/**
 *
 * @author Zully
 */
public class MaestroAlmacenes extends javax.swing.JInternalFrame {

    private ModeloTablaAlmacenes modelo;
    private TableFilterHeader filterHeader;

    /** Creates new form MaestroAlmacenes */
    public MaestroAlmacenes() {
        initComponents();
        modelo = new ModeloTablaAlmacenes();
        TableSorter tableSorter = new TableSorter(modelo, tablaAlmacenes.getTableHeader());
        tablaAlmacenes.setModel(tableSorter);
        filterHeader = new TableFilterHeader();
        filterHeader.setTable(tablaAlmacenes);
        RenderizadorTabla render = new RenderizadorTabla();
        tablaAlmacenes.setDefaultRenderer(String.class, render);
        this.llenarAlmacenes();
        setFrameIcon(new ImageIcon(getClass().getResource("/com/camachosanchez/recursos/table.png")));
        GUIUtil.fondoBarraHerramientas(jToolBar1);
        this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        this.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter() {

            @Override
            public void internalFrameClosing(InternalFrameEvent e) {
                cerrar();
            }
        });
    }

    private void cerrar() {
        GUIUtil.maximizarTodasLasVentanas(this);
    }

    private void setColumnaOculta() {
        tablaAlmacenes.getColumnModel().getColumn(1).setMinWidth(0);
        tablaAlmacenes.getColumnModel().getColumn(1).setMaxWidth(0);
        tablaAlmacenes.getColumnModel().getColumn(1).setPreferredWidth(0);
    }

//    private void setAnchoColumnas() {
////        int anchoColumna = 0;
//        TableColumnModel modeloColumna = jTableAlmacenes.getColumnModel();
//        TableColumn columnaTabla;
//        for (int i = 0; i < jTableAlmacenes.getColumnCount(); i++) {
//            columnaTabla = modeloColumna.getColumn(i);
//            switch (i) {
//                case 0:
//                    columnaTabla.setMinWidth(25);
//                    columnaTabla.setMaxWidth(25);
//                    columnaTabla.setPreferredWidth(25);
//                    break;
//                case 1:
//                    columnaTabla.setMinWidth(0);
//                    columnaTabla.setMaxWidth(0);
//                    columnaTabla.setPreferredWidth(0);
//                    break;
////                case 2:
////                    anchoColumna = 250;
////                    break;
////                case 3:
////                    anchoColumna = 100;
////                    break;
//                case 4:
////                    columnaTabla.setMinWidth(100);
////                    columnaTabla.setMaxWidth(100);
//                    columnaTabla.setPreferredWidth(100);
//                    break;
//            }
////            
//        }
//    }
    public void llenarAlmacenes() {
        modelo.limpiar();
        AlmacenesJdbcControler daoJdbc = new AlmacenesJdbcControler();
        List<Almacenes> almacenes = daoJdbc.findAll();
        for (Almacenes a : almacenes) {
            modelo.agregarAlmacen(a);
        }
        GUIUtil.SetAnchoColumnas(tablaAlmacenes);
        setColumnaOculta();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jPanel3 = new javax.swing.JPanel();
        btnNuevo = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaAlmacenes = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));
        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle(".:: Almacenes ::.");
        setPreferredSize(new java.awt.Dimension(847, 439));

        jPanel2.setLayout(new java.awt.BorderLayout());

        jToolBar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 4, 4));

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/camachosanchez/recursos/24_new.png"))); // NOI18N
        btnNuevo.setToolTipText("Nuevo");
        btnNuevo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        btnNuevo.setFocusable(false);
        btnNuevo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnNuevo.setMaximumSize(new java.awt.Dimension(22, 22));
        btnNuevo.setMinimumSize(new java.awt.Dimension(22, 22));
        btnNuevo.setPreferredSize(new java.awt.Dimension(22, 22));
        btnNuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });
        jPanel3.add(btnNuevo);

        btnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/camachosanchez/recursos/24_edit.png"))); // NOI18N
        btnModificar.setToolTipText("Modificar");
        btnModificar.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        btnModificar.setFocusable(false);
        btnModificar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnModificar.setMaximumSize(new java.awt.Dimension(22, 22));
        btnModificar.setMinimumSize(new java.awt.Dimension(22, 22));
        btnModificar.setPreferredSize(new java.awt.Dimension(22, 22));
        btnModificar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });
        jPanel3.add(btnModificar);

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/camachosanchez/recursos/24_delete.png"))); // NOI18N
        btnEliminar.setToolTipText("Eliminar");
        btnEliminar.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        btnEliminar.setFocusable(false);
        btnEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnEliminar.setMaximumSize(new java.awt.Dimension(22, 22));
        btnEliminar.setMinimumSize(new java.awt.Dimension(22, 22));
        btnEliminar.setPreferredSize(new java.awt.Dimension(22, 22));
        btnEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        jPanel3.add(btnEliminar);

        jToolBar1.add(jPanel3);

        jPanel2.add(jToolBar1, java.awt.BorderLayout.PAGE_START);

        jPanel1.setLayout(new java.awt.BorderLayout());

        tablaAlmacenes.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(156, 161, 165)));
        tablaAlmacenes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tablaAlmacenes.setRowHeight(22);
        tablaAlmacenes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tablaAlmacenes);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
    // TODO add your handling code here:
    int fila = tablaAlmacenes.getSelectedRow();
    if (fila > -1) {
        if (JOptionPane.showConfirmDialog(null, "¿Está seguro que desea eliminar el almacén seleccionado?", "Confirmación", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            int id = Integer.parseInt(tablaAlmacenes.getValueAt(fila, 1).toString());
            AlmacenesJdbcControler daoJdbc = new AlmacenesJdbcControler();
            byte estado = daoJdbc.eliminarAlmacen(id);
            if (estado == 1) {
                JOptionPane.showMessageDialog(this, "El almacén se encuentra relacionado con otros registros, no puede ser eliminado.", "Error en la eliminación", JOptionPane.ERROR_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "El almacén se ha eliminado de forma segura.", "Operación finalizada", JOptionPane.INFORMATION_MESSAGE);
                this.borrarAlmacen(fila);
            }
        }
    } else {
        JOptionPane.showMessageDialog(this, "Debe seleccionar un almacén.", "Advertencia", JOptionPane.WARNING_MESSAGE);
    }
}//GEN-LAST:event_btnEliminarActionPerformed

private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
    // TODO add your handling code here:
    int fila = tablaAlmacenes.getSelectedRow();
    if (fila > -1) {
//        int id = modelo.obtenerId(fila);
        int id = Integer.parseInt(tablaAlmacenes.getValueAt(fila, 1).toString());
        AlmacenesJpaController dao_almacen = new AlmacenesJpaController(null);
        EditorAlmacen editor = new EditorAlmacen(this, Principal.getInstance(), true, dao_almacen.findAlmacenes(id), fila);
        editor.setEstado(EstadoJDialog.MODIFICAR);
        editor.setVisible(true);
    } else {
        JOptionPane.showMessageDialog(this, "Debe seleccionar un almacén.", "Advertencia", JOptionPane.WARNING_MESSAGE);
    }
}//GEN-LAST:event_btnModificarActionPerformed

private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
    // TODO add your handling code here:
    EditorAlmacen registro = new EditorAlmacen(this, Principal.getInstance(), true, null, 0);
    registro.setEstado(EstadoJDialog.NUEVO);
    registro.setVisible(true);
}//GEN-LAST:event_btnNuevoActionPerformed

    public void borrarAlmacen(int fila) {
        modelo.borrarAccion(fila);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable tablaAlmacenes;
    // End of variables declaration//GEN-END:variables
}
